/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IMockDSService;
import org.cx1.service.impl.MListingService;
import org.cx1.service.impl.MockDSService;
import org.cx1.service.utils.scheduled.ScheduledServices;
import org.cx1.web.controller.action.MockDSAction;
import org.cx1.web.controller.action.ServicesAction;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class ServicesController extends BaseController {
        
    public ServicesController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();
        
        boolean loggedin = false;
        String userID = SessionUtils.getUserID(request.getSession());   
        
        if (!StringUtils.isEmpty(userID)) {
            loggedin = true;
            map.put("loggedin", loggedin);
        }
              
        int action = StringUtils.isEmpty( request.getParameter("a") ) ? 0 : Integer.parseInt(request.getParameter("a"));        
        if (StringUtils.isEmpty(request.getParameter("ref")) && !StringUtils.equalsIgnoreCase(request.getParameter("ref"), "hamday")) {
            action = 0;
            //mav.setView(new RedirectView("services.htm"));
        }
        
        switch (action) {
            
            
            case ServicesAction.START_MOCKDS_BACKUP_SERVICE: {                     
                if (loggedin) {
                    startMockDSBackupService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case ServicesAction.STOP_MOCKDS_BACKUP_SERVICE: {                     
                if (loggedin) {
                    stopMockDSBackupService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
                
                
            case ServicesAction.START_SEND_EMAIL_INVITE_SERVICE: {                    
                if (loggedin) {
                    startSendEmailInviteService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case ServicesAction.STOP_SEND_EMAIL_INVITE_SERVICE: {                    
                if (loggedin) {
                    stopSendEmailInviteService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
                
                
            case ServicesAction.START_LISTING_EXPIRY_CHECK_SERVICE: {                     
                if (loggedin) {
                    startListingExpiryCheckService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case ServicesAction.STOP_LISTING_EXPIRY_CHECK_SERVICE: {                     
                if (loggedin) {
                    stopListingExpiryCheckService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
                
                
            case ServicesAction.START_LISTING_STATS_SERVICE: {                     
                if (loggedin) {
                    startListingStatsService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case ServicesAction.STOP_LISTING_STATS_SERVICE: {                     
                if (loggedin) {
                    stopListingStatsService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }    
                
             
            case ServicesAction.START_LISTING_BOOST_SERVICE: {                     
                if (loggedin) {
                    startListingBoostService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
            case ServicesAction.STOP_LISTING_BOOST_SERVICE: {                     
                if (loggedin) {
                    stopListingBoostService(mav, map, request);
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
                
                
            default: {                
                if (loggedin) {     
                    //mav.setView(new RedirectView("services.htm"));
                }
                else {
                    mav.setView(new RedirectView("home.htm"));
                    return mav;                    
                }
                break;
            }
        }        
                
        map.put("mockDSBackupServiceRunning", ScheduledServices.isMockDSBackupServiceRunning());
        map.put("mockDSBackupServiceStartDate", ScheduledServices.getMockDSBackupServiceStartDate());
        map.put("mockDSBackupServiceInterval", ScheduledServices.getMockDSBackupServiceInterval());
        
        map.put("sendEmailInviteServiceRunning", ScheduledServices.isSendEmailInviteServiceRunning());
        map.put("sendEmailInviteServiceStartDate", ScheduledServices.getSendEmailInviteServiceStartDate());
        map.put("sendEmailInviteServiceInterval", ScheduledServices.getSendEmailInviteServiceInterval());
        map.put("sendEmailInviteServiceLastRun", ScheduledServices.getSendEmailInviteServiceLastRun());
        
        map.put("listingExpiryCheckServiceRunning", ScheduledServices.isListingExpiryCheckServiceRunning());
        map.put("listingExpiryCheckServiceStartDate", ScheduledServices.getListingExpiryCheckServiceStartDate());
        map.put("listingExpiryCheckServiceInterval", ScheduledServices.getListingExpiryCheckServiceInterval());
        map.put("listingExpiryCheckServiceLastRun", ScheduledServices.getListingExpiryCheckServiceLastRun());
                
        map.put("listingStatsServiceRunning", ScheduledServices.isListingStatsServiceRunning());
        map.put("listingStatsServiceStartDate", ScheduledServices.getListingStatsServiceStartDate());
        map.put("listingStatsServiceInterval", ScheduledServices.getListingStatsServiceInterval());
        map.put("listingStatsServiceLastRun", ScheduledServices.getListingStatsServiceLastRun());
        
        map.put("listingBoostServiceRunning", ScheduledServices.isListingBoostServiceRunning());
        map.put("listingBoostServiceStartDate", ScheduledServices.getListingBoostServiceStartDate());
        map.put("listingBoostServiceInterval", ScheduledServices.getListingBoostServiceInterval());
        map.put("listingBoostServiceLastRun", ScheduledServices.getListingBoostServiceLastRun());
        
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        
        mav.addObject("model", map);
        return mav;
    }
    
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    
    
    private void startMockDSBackupService(ModelAndView mav, Map map, HttpServletRequest request) {
        String strStartDate = request.getParameter("startDate");
        String strInterval = request.getParameter("interval");
        
        Date startDate = new Date();
        long interval = 86400000;
        
        if (!StringUtils.isEmpty(strStartDate)) {
            try {
                startDate = dateFormat.parse(strStartDate);
            } catch (Exception ex) { } 
        }
        if (!StringUtils.isEmpty(strInterval)) {
            try {
                interval = Integer.parseInt(strInterval);
            } catch (Exception ex) { }
        }
        ScheduledServices.startMockDSBackupService(startDate, interval);        
        mav.setView( new RedirectView("services.htm") );
    }
    
    private void stopMockDSBackupService(ModelAndView mav, Map map, HttpServletRequest request) {
        ScheduledServices.stopMockDSBackupService();
        mav.setView( new RedirectView("services.htm") );
    }
        
    
    
    
    
    private void startSendEmailInviteService(ModelAndView mav, Map map, HttpServletRequest request) {
        String strStartDate = request.getParameter("startDate");
        String strInterval = request.getParameter("interval");
        
        Date startDate = new Date();
        long interval = 86400000; //Default 1 Day
        
        if (!StringUtils.isEmpty(strStartDate)) {
            try {
                startDate = dateFormat.parse(strStartDate);
            } catch (Exception ex) { } 
        }
        if (!StringUtils.isEmpty(strInterval)) {
            try {
                interval = Integer.parseInt(strInterval);
            } catch (Exception ex) { }
        }
        ScheduledServices.startSendEmailInviteService(startDate, interval);
        mav.setView( new RedirectView("services.htm") );
    }
    
    private void stopSendEmailInviteService(ModelAndView mav, Map map, HttpServletRequest request) {
        ScheduledServices.stopSendEmailInviteService();
        mav.setView( new RedirectView("services.htm") );
    }
    
    
    
    
    
    private void startListingExpiryCheckService(ModelAndView mav, Map map, HttpServletRequest request) {
        String strStartDate = request.getParameter("startDate");
        String strInterval = request.getParameter("interval");
        
        Date startDate = new Date();
        long interval = 604800000;
        
        if (!StringUtils.isEmpty(strStartDate)) {
            try {
                startDate = dateFormat.parse(strStartDate);
            } catch (Exception ex) { } 
        }
        if (!StringUtils.isEmpty(strInterval)) {
            try {
                interval = Integer.parseInt(strInterval);
            } catch (Exception ex) { }
        }
        ScheduledServices.startListingExpiryCheckService(startDate, interval);
        mav.setView( new RedirectView("services.htm") );
    }
    
    private void stopListingExpiryCheckService(ModelAndView mav, Map map, HttpServletRequest request) {
        ScheduledServices.stopListingExpiryCheckSService();
        mav.setView( new RedirectView("services.htm") );
    }
        
    
    
    
    private void startListingStatsService(ModelAndView mav, Map map, HttpServletRequest request) {
        String strStartDate = request.getParameter("startDate");
        String strInterval = request.getParameter("interval");
        
        Date startDate = new Date();
        long interval = 604800000;
        
        if (!StringUtils.isEmpty(strStartDate)) {
            try {
                startDate = dateFormat.parse(strStartDate);
            } catch (Exception ex) { } 
        }
        if (!StringUtils.isEmpty(strInterval)) {
            try {
                interval = Integer.parseInt(strInterval);
            } catch (Exception ex) { }
        }
        ScheduledServices.startListingStatsService(startDate, interval);
        mav.setView( new RedirectView("services.htm") );
    }
        
    private void stopListingStatsService(ModelAndView mav, Map map, HttpServletRequest request) {
        ScheduledServices.stopListingStatsService();
        mav.setView( new RedirectView("services.htm") );
    }
    
    
    
    
    
    private void startListingBoostService(ModelAndView mav, Map map, HttpServletRequest request) {
        String strStartDate = request.getParameter("startDate");
        String strInterval = request.getParameter("interval");
        
        Date startDate = new Date();
        long interval = 604800000;
        
        if (!StringUtils.isEmpty(strStartDate)) {
            try {
                startDate = dateFormat.parse(strStartDate);
            } catch (Exception ex) { } 
        }
        if (!StringUtils.isEmpty(strInterval)) {
            try {
                interval = Integer.parseInt(strInterval);
            } catch (Exception ex) { }
        }
        ScheduledServices.startListingBoostService(startDate, interval);
        mav.setView( new RedirectView("services.htm") );
    }
        
    private void stopListingBoostService(ModelAndView mav, Map map, HttpServletRequest request) {
        ScheduledServices.stopListingBoostService();
        mav.setView( new RedirectView("services.htm") );
    }
    
    
    
}
